'use strict'

module.exports = [
  {
    pre: true,
    middleware: async (c, next) => {
      let token = c.headers.authorization || c.query.token

      if (!token) {
        return c.status(401).send('!token')
      }

      let uinfo = c.service.tok.verify(token)

      if (!uinfo.ok) {
        return c.status(401).send(uinfo.errcode)
      }

      c.box.user = uinfo.data

      await next()
    }
  }
]
